package cn.gemiman.daoyi.module.iot.controller.admin.jhlequipmentplace;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;
import cn.gemiman.daoyi.module.iot.controller.admin.jhlequipmentplace.vo.JhlEquipmentPlacePageReqVO;
import cn.gemiman.daoyi.module.iot.controller.admin.jhlequipmentplace.vo.JhlEquipmentPlaceRespVO;
import cn.gemiman.daoyi.module.iot.controller.admin.jhlequipmentplace.vo.JhlEquipmentPlaceSaveReqVO;
import cn.gemiman.daoyi.module.iot.dal.dataobject.jhlequipmentplace.JhlEquipmentPlaceDO;
import cn.gemiman.daoyi.module.iot.service.jhlequipmentplace.JhlEquipmentPlaceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 精华隆场所信息")
@RestController
@RequestMapping("/iot/jhl-equipment-place")
@Validated
public class JhlEquipmentPlaceController {

    @Resource
    private JhlEquipmentPlaceService jhlEquipmentPlaceService;

    @PutMapping("/async")
    @Operation(summary = "同步精华隆场所信息")
    public CommonResult<Integer> asyncJhlEquipmentPlace() {
        return success(jhlEquipmentPlaceService.asyncJhlEquipmentPlace());
    }

    @PostMapping("/create")
    @Operation(summary = "创建精华隆场所信息")
    @PreAuthorize("@ss.hasPermission('iot:jhl-equipment-place:create')")
    public CommonResult<Integer> createJhlEquipmentPlace(@Valid @RequestBody JhlEquipmentPlaceSaveReqVO createReqVO) {
        return success(jhlEquipmentPlaceService.createJhlEquipmentPlace(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新精华隆场所信息")
    @PreAuthorize("@ss.hasPermission('iot:jhl-equipment-place:update')")
    public CommonResult<Boolean> updateJhlEquipmentPlace(@Valid @RequestBody JhlEquipmentPlaceSaveReqVO updateReqVO) {
        jhlEquipmentPlaceService.updateJhlEquipmentPlace(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除精华隆场所信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:jhl-equipment-place:delete')")
    public CommonResult<Boolean> deleteJhlEquipmentPlace(@RequestParam("id") Integer id) {
        jhlEquipmentPlaceService.deleteJhlEquipmentPlace(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得精华隆场所信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:jhl-equipment-place:query')")
    public CommonResult<JhlEquipmentPlaceRespVO> getJhlEquipmentPlace(@RequestParam("id") Integer id) {
        JhlEquipmentPlaceDO jhlEquipmentPlace = jhlEquipmentPlaceService.getJhlEquipmentPlace(id);
        return success(BeanUtils.toBean(jhlEquipmentPlace, JhlEquipmentPlaceRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "post获得精华隆场所信息分页")
    @PreAuthorize("@ss.hasPermission('iot:jhl-equipment-place:query')")
    public CommonResult<PageResult<JhlEquipmentPlaceRespVO>> postJhlEquipmentPlacePage(@Valid @RequestBody JhlEquipmentPlacePageReqVO pageReqVO) {
        return getJhlEquipmentPlacePage(pageReqVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得精华隆场所信息分页")
    @PreAuthorize("@ss.hasPermission('iot:jhl-equipment-place:query')")
    public CommonResult<PageResult<JhlEquipmentPlaceRespVO>> getJhlEquipmentPlacePage(@Valid JhlEquipmentPlacePageReqVO pageReqVO) {
        PageResult<JhlEquipmentPlaceDO> pageResult = jhlEquipmentPlaceService.getJhlEquipmentPlacePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JhlEquipmentPlaceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出精华隆场所信息 Excel")
    @PreAuthorize("@ss.hasPermission('iot:jhl-equipment-place:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJhlEquipmentPlaceExcel(@Valid JhlEquipmentPlacePageReqVO pageReqVO,
                                             HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JhlEquipmentPlaceDO> list = jhlEquipmentPlaceService.getJhlEquipmentPlacePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "精华隆场所信息.xls", "数据", JhlEquipmentPlaceRespVO.class,
                BeanUtils.toBean(list, JhlEquipmentPlaceRespVO.class));
    }

}
